package com.youth.controller.fileupload;

import java.text.DecimalFormat;

import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.ProgressListener;

public class FileUploadProgressListener implements ProgressListener {

	private HttpSession session;

	public FileUploadProgressListener(HttpSession session) {
		this.session = session;
	}

    // pBytesRead 已经上传的大小
    // pContentLength 文件总大小
	// pItems 目前正在读取第几个文件
	@Override
	public void update(long pBytesRead, long pContentLength, int pItems) {
		double percent = ((double) pBytesRead / pContentLength) * 100;
		DecimalFormat df = new DecimalFormat("#.00");
		String percentStr = df.format(percent);
		session.setAttribute("progress", percentStr);
	}
}